import { defineStore } from "pinia";
import { PageVariable } from "@/datav/types/editor";
import { getTextParams } from "@/utils";

export interface IEventState {
	componentsView: PageVariable["componentsView"];
	publishersView: PageVariable["publishersView"];
	subscribersView: PageVariable["subscribersView"];
	variables: Record<string, any>;
}

export const useEventStore = defineStore("event", {
	state: (): IEventState => ({
		componentsView: {},
		publishersView: {},
		subscribersView: {},
		variables: {}
	}),
	actions: {
		setPublishersView(id: string, keys: string[], enable: boolean) {
			const pv = this.publishersView;
			const pvkeys = Object.keys(pv);
			const allKeys = new Set([...keys, ...pvkeys]);
			allKeys.forEach(key => {
				if (enable) {
					if (!pvkeys.includes(key)) {
						pv[key] = [id];
					} else if (!pv[key].includes(id)) {
						pv[key].push(id);
					} else if (!keys.includes(key)) {
						pv[key] = pv[key].filter(m => m !== id);
					}
				} else {
					if (pvkeys.includes(key)) {
						pv[key] = pv[key].filter(m => m !== id);
					}
				}

				if (pv[key].length === 0) {
					delete pv[key];
				}
			});
		},
		setSubscribersView(id: string, data: string) {
			const sv = this.subscribersView;
			const keys = getTextParams(data).map(m => m.substring(1));
			const svkeys = Object.keys(sv);
			const allKeys = new Set([...keys, ...svkeys]);
			for (const key of allKeys) {
				if (keys.length > 0) {
					if (!svkeys.includes(key)) {
						sv[key] = [id];
					} else if (!sv[key].includes(id)) {
						sv[key].push(id);
					} else if (!keys.includes(key)) {
						sv[key] = sv[key].filter(m => m !== id);
					}
				} else {
					if (svkeys.includes(key)) {
						sv[key] = sv[key].filter(m => m !== id);
					}
				}

				if (sv[key].length === 0) {
					delete sv[key];
				}
			}
		},
		async setVariables(fields: Record<string, string>, data: Record<string, any>) {
			const res = {};
			for (const key in fields) {
				const alias = fields[key] || key;
				// @ts-ignore
				res[alias] = data[key];
			}

			this.variables = {
				...this.variables,
				...res
			};
		}
	}
});
